//
//  DBLoginViewController.m
//  Bottle
//
//  Created by apple on 11-11-12.
//  Copyright (c) 2011年 __MyCompanyName__. All rights reserved.
//

#import "DBLoginViewController.h"
#import "KEY.h"

@implementation DBLoginViewController

@synthesize delegate;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        _webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 44, 320, 416)];
        [self.view addSubview:_webView];
        _webView.delegate = self;
        
        UINavigationBar *navigateBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
        UINavigationItem *navigateItem = [[UINavigationItem alloc] initWithTitle:@"豆瓣"];
        UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancel:)];
        navigateItem.leftBarButtonItem = cancelButton;
        [cancelButton release];
        [navigateBar pushNavigationItem:navigateItem animated:NO];
        [navigateItem release];
        [self.view addSubview:navigateBar];
        [navigateBar release];
        
    }
    return self;
}

- (void)cancel:(id)sender
{
    [self dismissModalViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

- (void)dealloc
{
    delegate = nil;
    [_webView   release];
    _webView = nil;
    [super  dealloc];
}

#pragma mark - View lifecycle

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView
{
}
*/

/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
    [super viewDidLoad];
}
*/

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (void)loadUrl:(NSString*)urlString
{
    [_webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlString]]];
}

#pragma mark - web view delegate

- (void)webViewDidStartLoad:(UIWebView *)webView
{
    NSLog(@"url : %@" , [webView.request URL]);
    NSString *string = [[webView.request URL] description];
    NSRange range = [string rangeOfString:CallBackURL];
    if (range.length != 0 && range.location == 0) {
        _webView.hidden = YES;
        [_webView stopLoading];
        [self dismissModalViewControllerAnimated:YES];
        if ([delegate respondsToSelector:@selector(loginSuccess:)]) {
            NSRange range = [string rangeOfString:@"oauth_token="];
            NSString *oauthToken = [string substringFromIndex:range.location + range.length];
            [delegate loginSuccess:oauthToken];
        }
    }
}

@end
